/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cnd;

import javax.swing.table.*;

/**
 *
 * @author marten
 */
public class SearchResultTableModel extends AbstractTableModel {

    SearchResults searchResultList;
    String[] columns;
    public SearchResultTableModel(SearchResults searchResultList) {
        this.searchResultList = searchResultList;
        
        columns = new String[]{"Rank", "Name", "Size", "Online", "Folder"};
    }

    @Override
    public int getRowCount() {
        //return 1;
        return searchResultList.getLength();
    }

    @Override
    public int getColumnCount() {
        return columns.length;
    }

    @Override
    public String getColumnName(int columnIndex) {
        return columns[columnIndex];
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return String.class;
        //        return classes[columnIndex];
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        //return "test";
        switch (columnIndex) {
            case 0:
                return searchResultList.getSearchResult(rowIndex).rank;
            case 1:
                return searchResultList.getSearchResult(rowIndex).name;
            case 2:
                return fileSizeString(searchResultList.getSearchResult(rowIndex).size);
            case 3:
                return searchResultList.getSearchResult(rowIndex).online;
            case 4:
                return !searchResultList.getSearchResult(rowIndex).file;
            default:
                return "";
        }
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
    }
    public String fileSizeString(long bytes){
        long tb = (long) Math.pow(10, 12);
        long gb = (long) Math.pow(10, 9);
        long mb = (long) Math.pow(10, 6);
        long kb = (long) Math.pow(10, 3);
        if(bytes>tb){
            return ((double) Math.round((double) bytes/tb*100)/100) + " TB";
        }else if(bytes>gb){
            return ((double) Math.round((double) bytes/gb*100)/100) + " GB";
        }else if(bytes>mb){
            return ((double) Math.round((double) bytes/mb*100)/100) + " MB";
        }else if(bytes>kb){
            return ((double) Math.round((double) bytes/kb*100)/100) + " KB";
        }else{
            return bytes + " B";
        }
    }

    public SearchResult getSearchResult(int row){
        return searchResultList.getSearchResult(row);
    }
}
